
package com.jf.cloud.service.sysmanager.remote;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgBaseHelpDocumentService;
import com.jf.cloud.service.sysmanager.entity.PubgBaseHelpDocument;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 帮助文档控制器
 *
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgBaseHelpDocument")
public class PubgBaseHelpDocumentController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBaseHelpDocumentController.class);

	/**
	 * 帮助文档服务
	 */
	@Resource
	private IPubgBaseHelpDocumentService pubgBaseHelpDocumentService;

	/**
	 * 获取帮助文档列表数据
	 *
	 * @author 高冬冬 创建时间：2017年12月14日
	 */
	@ResponseBody
	@RequestMapping("/data.json")
	public AjaxPageData<PubgBaseHelpDocument> getBaseHelpDocumentList(HttpServletRequest request, OPaging<PubgBaseHelpDocument> page){
		AjaxPageData<PubgBaseHelpDocument> pageData = new AjaxPageData<PubgBaseHelpDocument>();
		try {
			page.getWm().put("DEL_FLAG",0);
			pageData = getAjaxPageData(pubgBaseHelpDocumentService.findPage(page));
		} catch (Exception e) {
			log.error("获取帮助文档列表数据出错了" , e);
			request.setAttribute("msg", "获取帮助文档列表数据出错了");
		}
		return pageData;
	}

	/**
	 * 获取帮助文档信息
	 *
	 * @author 高冬冬 创建时间：2017年12月15日
	 */
	@ResponseBody
	@RequestMapping("/getBaseHelpDocumentInfo")
	public PubgBaseHelpDocument getBaseHelpDocumentInfo(Long rowId, HttpRequest request){
		PubgBaseHelpDocument pubgBaseHelpDocument = new PubgBaseHelpDocument();
		try {
			// 修改帮助文档信息时
			if (rowId != null && rowId.longValue() > 0) {
				pubgBaseHelpDocument = pubgBaseHelpDocumentService.findByPK(rowId);
			}
		} catch (Exception e) {
			log.error("获取帮助文档信息出错！", e);
		}
		return pubgBaseHelpDocument;
	}

	/**
	 * 添加帮助文档
	 *
	 * @author 高冬冬 创建时间：2017年12月15日
	 */
	@ResponseBody
	@RequestMapping("/save")
	public String saveBaseHelpDocument(HttpServletRequest request, HttpServletResponse response, PubgBaseHelpDocument pubgBaseHelpDocument){
		Result<PubgBaseHelpDocument> result = new Result<PubgBaseHelpDocument>();
		result.setCode(-1);
		result.setMessage("添加帮助文档出错了");
		try{
			SysStaff user = getLoginUser(request);
			if (user == null) {
				result.setCode(-2);
				return result.toString();
			}
			if ("1".equals(request.getParameter("isReCommit"))) {
				result.setMessage("请勿重复提交！");
				return result.toString();
			}

			if (Long.valueOf(0).equals(pubgBaseHelpDocument.getRowId())) {
				pubgBaseHelpDocument.setRowId(null);
			}
			pubgBaseHelpDocument.setDelFlag(0);
			pubgBaseHelpDocument.setCreateId(user.getRowId());
			pubgBaseHelpDocument.setCreateAccount(user.getStaffAccount());
			pubgBaseHelpDocument.setCreateTime(DateUtil.getCurrentDateTime());
			pubgBaseHelpDocument.setOptId(user.getRowId());
			pubgBaseHelpDocument.setOptAccount(user.getStaffAccount());
			pubgBaseHelpDocument.setOptTime(DateUtil.getCurrentDateTime());
			pubgBaseHelpDocumentService.saveOrUpdatePubgBaseHelpDocument(pubgBaseHelpDocument);
			result.setCode(0);
			result.setMessage("添加帮助文档成功！");
		} catch (Exception e) {
			log.error("添加帮助文档出错了", e);
			return result.toString();
		}
		return result.toString();
	}

	/**
	 * 修改帮助文档
	 *
	 * @author 高冬冬 创建时间：2017年12月15日
	 */
	@ResponseBody
	@RequestMapping("/update")
	public String updateBaseHelpDocument(HttpServletRequest request, HttpServletResponse response, PubgBaseHelpDocument pubgBaseHelpDocument) {
		Result<PubgBaseHelpDocument> result = new Result<PubgBaseHelpDocument>();
		result.setCode(-1);
		result.setMessage("修改帮助文档出错了");
		try{
			SysStaff user = getLoginUser(request);
			if (user == null) {
				result.setCode(-2);
				return result.toString();
			}
			if ("1".equals(request.getParameter("isReCommit"))) {
				result.setMessage("请勿重复提交！");
				return result.toString();
			}

			if (pubgBaseHelpDocument.getOrderByNo() == null || pubgBaseHelpDocument.getOrderByNo().intValue() == 0 ) {
				result.setMessage("请输入排序编号！");
				return result.toString();
			}
			pubgBaseHelpDocument.setOptId(user.getRowId());
			pubgBaseHelpDocument.setOptAccount(user.getStaffAccount());
			pubgBaseHelpDocument.setOptTime(DateUtil.getCurrentDateTime());
			pubgBaseHelpDocumentService.updatePubgBaseHelpDocument(pubgBaseHelpDocument);
			result.setCode(1);
            result.setMessage("修改帮助文档成功！");
		} catch (Exception e) {
			log.error("修改帮助文档出错了", e);
			return result.toString();
		}
		return result.toString();
	}

	/**
	 * 删除帮助文档（更新标记）
	 *
	 * @author 高冬冬 创建时间：2017年12月15日
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteBaseHelpDocument(HttpServletRequest request, Long rowId, Integer openFlag) {
		String result = "-2";
		try {
			// 验证登录状态
            SysStaff user = getLoginUser(request);
			if (user == null) {
				return result;
			}
			if (openFlag == 1) {
				return "3";
			}
			// 默认更新失败
			result = "2";
            PubgBaseHelpDocument pubgBaseHelpDocument = new PubgBaseHelpDocument();
            pubgBaseHelpDocument.setRowId(rowId);
            pubgBaseHelpDocument.setDelFlag(1);
            pubgBaseHelpDocument.setOptId(user.getRowId());
            pubgBaseHelpDocument.setOptAccount(user.getStaffAccount());
            pubgBaseHelpDocument.setOptTime(DateUtil.getCurrentDateTime());
            pubgBaseHelpDocumentService.updatePubgBaseHelpDocument(pubgBaseHelpDocument);
            result = "1";
		} catch (Exception e) {
			log.error("删除帮助文档出错了", e);
		}
		return result;
	}

	/**
	 * 修改帮助文档启用状态
	 *
	 * @author 高冬冬 创建时间：2017年12月20日
	 */
	@ResponseBody
	@RequestMapping("/updateOpenFlag")
	public String updateOpenFlag(HttpServletRequest request, Long rowId, Integer openFlag) {
		String result = "-2";
		try {
			if (rowId == null || openFlag == null) {
				return "2";
			}
			// 验证登录状态
            SysStaff user = getLoginUser(request);
			if (user == null) {
				return result;
			}
			// 默认更新失败
			result = "2";
            PubgBaseHelpDocument pubgBaseHelpDocument = new PubgBaseHelpDocument();
            pubgBaseHelpDocument.setRowId(rowId);
            pubgBaseHelpDocument.setOpenFlag(openFlag);
            pubgBaseHelpDocument.setOptId(user.getRowId());
            pubgBaseHelpDocument.setOptAccount(user.getStaffAccount());
            pubgBaseHelpDocument.setOptTime(DateUtil.getCurrentDateTime());
            pubgBaseHelpDocumentService.updatePubgBaseHelpDocument(pubgBaseHelpDocument);
            result = "1";
		} catch (Exception e) {
			log.error("修改帮助文档启用状态出错了", e);
		}
		return result;
	}

}
